package listeners

import (
	"encoding/json"
	"errors"
	"fmt"
)

const (
	Stack           string = "Stack"
	Metrics         string = "Metrics"
	ContractAccount string = "ContractAccount"
	Msp             string = "Msp"
	Chaincode       string = "Chaincode"
	Cci             string = "Cci"
	Genesis         string = "Genesis"
	Wallet          string = "Wallet"
	Ns3             string = "Ns3"
)

type ErrorMessage struct {
	Mod  string `json:"mod"`
	Code int    `json:"code"`
	Func string `json:"func"`
	Text string `json:"text"`
}

func NewErrorMessage(mod string, code int, fun, text string) ErrorMessage {
	return ErrorMessage{mod, code, fun, text}
}

func (em ErrorMessage) Error() error {
	return errors.New(fmt.Sprintf("module: %v, code: %v, func: %v, text: %v", em.Mod, em.Code, em.Func, em.Text))
}

func (em ErrorMessage) Json() string {
	emBytes, err := json.Marshal(em)

	if err != nil {
		return ""
	} else {
		return string(emBytes)
	}
}
